package com.gdssoft.pdaweb.service.ws.impl;
import java.util.Date;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.model.CheckTask;
import com.gdssoft.pdaweb.model.CheckWorkOrder;
import com.gdssoft.pdaweb.model.PdaMdAssetDetail;
import com.gdssoft.pdaweb.model.PdaMdAssetInfo;
import com.gdssoft.pdaweb.model.PdaMeterOperate;
import com.gdssoft.pdaweb.model.RepairWorkOrder;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.dao.CheckTaskDao;
import com.gdssoft.pdaweb.dao.CheckWorkOrderDao;
import com.gdssoft.pdaweb.dao.CrmWorkOrderDao;
import com.gdssoft.pdaweb.dao.PdaMdAssetDetailDao;
import com.gdssoft.pdaweb.dao.PdaMdAssetInfoDao;
import com.gdssoft.pdaweb.dao.PdaMeterOperateDao;
import com.gdssoft.pdaweb.dao.RepairWorkOrderDao;
import com.gdssoft.pdaweb.dao.am.PdaAmMeterDao;
import com.gdssoft.pdaweb.dao.water.PdaWaterMeterDao;
import com.gdssoft.pdaweb.service.ws.EamService;
@WebService(endpointInterface = "com.gdssoft.pdaweb.service.ws.EamService", 
serviceName="EamService", targetNamespace="http://gdssoft.foxconn.com/")
public  class EamServiceImpl implements EamService{
	private CheckTaskDao checkTaskDao;
	private CheckWorkOrderDao checkWorkOrderDao;
	private RepairWorkOrderDao repairWorkOrderDao;
	private PdaMdAssetInfoDao pdaMdAssetInfoDao;
	private PdaMdAssetDetailDao pdaMdAssetDetailDao;
	private PdaMeterOperateDao pdaMeterOperateDao;
	private PdaWaterMeterDao pdaWaterMeterDao;
	private PdaAmMeterDao pdaAmMeterDao;
	
	private transient final Log logger = LogFactory.getLog(getClass());
	
	public EamServiceImpl(
			CheckTaskDao checkTaskDao,
			CheckWorkOrderDao checkWorkOrderDao,
			RepairWorkOrderDao  repairWorkOrderDao,
			PdaMdAssetInfoDao pdaMdAssetInfoDao,
			PdaMdAssetDetailDao pdaMdAssetDetailDao,
			PdaMeterOperateDao pdaMeterOperateDao,
			PdaWaterMeterDao pdaWaterMeterDao,
			PdaAmMeterDao pdaAmMeterDao
			) {
		this.checkTaskDao = checkTaskDao;
		this.checkWorkOrderDao = checkWorkOrderDao;
		this.repairWorkOrderDao=repairWorkOrderDao;
		this.pdaMdAssetInfoDao = pdaMdAssetInfoDao;
		this.pdaMdAssetDetailDao = pdaMdAssetDetailDao;
		this.pdaMeterOperateDao = pdaMeterOperateDao;
		this.pdaWaterMeterDao =  pdaWaterMeterDao;
		this.pdaAmMeterDao = pdaAmMeterDao;
	}
	
	public boolean pushInspectionWorkTask(
			String assignmentId,
			String taskid,
			String description ){
		boolean result = false;
		try{
			long assId =Long.parseLong(assignmentId);
			long tId =Long.parseLong(taskid);
			if(null==checkTaskDao.getByUniqueKey(assId, tId)){
				CheckTask checkTask = new CheckTask();
				checkTask.setAssignmentId(assId);
				checkTask.setTaskId(tId);
				checkTask.setDescription(description);
				checkTask.setStatus("1");
				checkTask.setCreatedBy("eamService");
				checkTask.setCreatedDt(new Date());
				checkTaskDao.save(checkTask);
				result=true;
			}
			else{
				logger.warn("巡检工单任务表中已经存在ASSIGNMENTID为" + assignmentId+"TSAKID为"+taskid+"的数据");
			}
		}
		catch(Exception e){
			logger.error("巡检工单任务表中中ASSIGNMENTID或者TASKID不是有效的数据类型", e);
		}
		return result;
	}
	
	
	public boolean pushInspectionWorkOrder(String assignmentId, String wonum,
			String description, String appointPeople, String appointDate,
			String siteId, String locationId, String locationDesc,
			String assetNo, String assetDesc, String reportedBy,
			String reportDate) {
		boolean result = false;
		try{
			long id = Long.parseLong(assignmentId);
			if(null==checkWorkOrderDao.get(id)){
				CheckWorkOrder  checkWorkOrder = new CheckWorkOrder();
				checkWorkOrder.setAssignmentID(Long.parseLong(assignmentId));
				checkWorkOrder.setWoNum(wonum);
				checkWorkOrder.setDescription(description);
				checkWorkOrder.setAppointPeople(appointPeople);
				checkWorkOrder.setAppointDate(appointDate);
				checkWorkOrder.setSiteID(siteId);
				checkWorkOrder.setLocationID(locationId);
				checkWorkOrder.setLocDescription(locationDesc);
				checkWorkOrder.setAssetNo(assetNo);
				checkWorkOrder.setAssetDescription(assetDesc);
				checkWorkOrder.setReportedBy(reportedBy);
				checkWorkOrder.setReportedDate(DateUtil.dateParse(reportDate));
				checkWorkOrder.setStatus("1");
				checkWorkOrder.setCreatedBy("eamService");
				checkWorkOrder.setCreatedDt(new Date());
				checkWorkOrderDao.save(checkWorkOrder);
			    result=true;
			}
			else{
				logger.warn("巡检工单基本信息表中已经存在ASSIGNMENTID为" + assignmentId+"的数据");
			}
		}catch(Exception e){
			logger.error("巡检工单基本信息表中ASSIGNMENTID不是有效的数据类型", e);
		} 
		return result;
	}

	@Override
	public boolean pushRepairWorkOrder(String assignmentId, String wonum,
			String description, String appointPeople, String appointDate,
			String siteId, String locationId, String locationDesc,
			String assetNo, String assetDesc, String reportedBy,
			String reportDate) {
		boolean result =false;
		
		try{
		    long id =Long.parseLong(assignmentId);
		
	     	if(null==repairWorkOrderDao.get(id)){
			    RepairWorkOrder repairWorkOrder = new RepairWorkOrder();
		        repairWorkOrder.setAssignmentId(Long.parseLong(assignmentId));
		     	repairWorkOrder.setWoNum(wonum);
		    	repairWorkOrder.setDescription(description);
			    repairWorkOrder.setAppointPeople(appointPeople);
			    repairWorkOrder.setAppointDate(appointDate);
			    repairWorkOrder.setSiteID(siteId);
			    repairWorkOrder.setLocationID(locationId);
			    repairWorkOrder.setLocDescription(locationDesc);
			    repairWorkOrder.setAssetNo(assetNo);
			    repairWorkOrder.setAssetDescription(assetDesc);
			    repairWorkOrder.setReportedBy(reportedBy);
			    repairWorkOrder.setReportedDate(DateUtil.parseDate(reportDate));
			    repairWorkOrder.setCreatedBy("eamService");
			    repairWorkOrder.setCreatedDt(new Date());
			    repairWorkOrder.setStatus("1");
			    repairWorkOrderDao.save(repairWorkOrder);
				result = true;
		     }
	     	else{
	     		logger.warn("维修工单基本信息表中已经存在ASSIGNMENTID为" + assignmentId+"的数据");}
		}
		catch(Exception e)
		{
			logger.error("维修工单基本信息表中ASSIGNMENTID不是有效的数据类型", e);
		}
		return result;
	}

	public boolean pushAssetInfo(
			String assetNo,
			String assetDesc,
			String houseId,
			String locationId,
			String location,
			String parent,
			String siteId,
			String status,
			String c_barCode,
			String classStructuredId,
			String isChange){
		boolean result =false;
		   if(null==pdaMdAssetInfoDao.getByBarCode(assetNo)){
				PdaMdAssetInfo pdaMdAssetInfo = new PdaMdAssetInfo();
				pdaMdAssetInfo.setAssetNo(assetNo);
				pdaMdAssetInfo.setAssetDesc(assetDesc);
				pdaMdAssetInfo.setHouseId(houseId);
				pdaMdAssetInfo.setLocationId(locationId);
				pdaMdAssetInfo.setIsSupply("N");
				pdaMdAssetInfo.setCreatedDt(new Date());
				pdaMdAssetInfo.setCreatedBy("eamService"); 
				pdaMdAssetInfo.setLocation(location);
				pdaMdAssetInfo.setParent(parent);
				pdaMdAssetInfo.setSiteId(siteId);
				pdaMdAssetInfo.setStatus(status);
				pdaMdAssetInfo.setBarCode(c_barCode);
				pdaMdAssetInfo.setClassStructureId(classStructuredId);
				pdaMdAssetInfo.setIsChange(isChange);	
				pdaMdAssetInfoDao.save(pdaMdAssetInfo);
			    result = true;
		   }	
		   else
		   {logger.warn("资产基本信息表中已经存在ASSETNO为" + assetNo+"的数据");}
		return result;	
	}
	@Override
	public boolean pushAssetDetail(String assetNum,
			                       String itemNum,
			                       String description,
			                       String siteId,
			                       String issueUnit)
	{
		boolean result = false;
		if(null!=pdaMdAssetInfoDao.getByAssetNumAndSiteId(assetNum, siteId)){
				PdaMdAssetDetail pdaMdAssetDetail = new PdaMdAssetDetail();
				pdaMdAssetDetail.setAssetNum(assetNum);
				pdaMdAssetDetail.setItemNum(itemNum);
				pdaMdAssetDetail.setDescription(description);
				pdaMdAssetDetail.setSiteId(siteId);
				pdaMdAssetDetail.setIssueUnit(issueUnit);
				pdaMdAssetDetail.setCreatedDt(new Date());
				pdaMdAssetDetail.setCreatedBy("eamService");
				try{pdaMdAssetDetailDao.save(pdaMdAssetDetail);
				result = true;
			}
			catch(Exception ex){
				logger.error("插入资产编号为" + assetNum+"，站点为"+ siteId+"备品备件信息出错!", ex);
				return result;
			}
		}
		else{
			logger.warn("资产基本信息表中没有资产编号为" + assetNum+"，站点为"+ siteId+"的数据");
		}
		return result;
	}
	
	public boolean alertRepairWorkStatus(String assignmentId,
            String status){
		boolean result =false;
		try{
			long id =Long.parseLong(assignmentId);
	     	if(null!=repairWorkOrderDao.get(id)){
	     		 RepairWorkOrder repairWorkOrder =repairWorkOrderDao.get(id);
	     		 repairWorkOrder.setStatus(status);
	     		 repairWorkOrder.setLastEditDt(new Date());
	     		 repairWorkOrder.setLastEditBy("eamService");
	     		 repairWorkOrderDao.save(repairWorkOrder);
	     		 result =true;
	     	}
	     	else{
	     		logger.warn("维修工单基本信息表中不存在ASSIGNMENTID为"+assignmentId+"数据!");
	     		return result;
	     	}
		
		  }
		catch(Exception ex){
			logger.error("维修工单基本信息表中ASSIGNMENTID不是有效的数据类型", ex);
		}
		return result;
	}
	public boolean pushMeterOperate(String barCode,
			String assetNum,
			String siteId,
			String oldBarCode,
	        String oldAssetNum,
            String oldSiteId,
            String operator,
            Date operateTime){
		boolean result = false;
		logger.info("往PDA_METER_OPERATE表中插入数据"+barCode+","+assetNum+","+siteId);
		List<PdaMdAssetInfo> list = pdaMdAssetInfoDao.getByAssetNumAndSiteId(assetNum, siteId);
		if(list.size()==0){
			logger.warn("PDA_MD_ASSET_INFO表中不存在资产条码为"+barCode+",资产编号为："+assetNum+",站点为："+siteId);
			return result;
		}
		
	 	if(list.get(0).getIsChange().equals("1")){
	 		if(pdaMeterOperateDao.getByAssetNumAndSiteId(oldAssetNum, oldSiteId).size()==0){
				 PdaMeterOperate PdaMeterOperate = new PdaMeterOperate();
				 PdaMeterOperate.setAssetNum(assetNum);
				 PdaMeterOperate.setBarCode(barCode);
				 PdaMeterOperate.setSiteId(siteId);
				 PdaMeterOperate.setOldAssetNum(oldAssetNum);
				 PdaMeterOperate.setOldBarCode(oldBarCode);
				 PdaMeterOperate.setOldSiteId(oldSiteId);
				 PdaMeterOperate.setOperator(operator);
				 PdaMeterOperate.setOperateTime(operateTime);
				 PdaMeterOperate.setCreatedBy("eamService");
				 PdaMeterOperate.setCreatedDt(new Date());
				 PdaMeterOperate.setLastEditedBy("eamService");
				 PdaMeterOperate.setLastEditedDt(new Date());
				 try{
					 pdaMeterOperateDao.merge(PdaMeterOperate);
					 result =true;
				 }
				 catch(Exception ex){
					 result = false;
					 logger.error("往PDA_METER_OPERATE表中插入数据失败", ex);
				 }
				 String  classStructureId = list.get(0).getClassStructureId();
				 if(classStructureId.equals("1")){
					 PdaWaterMeter pdaWaterMeter = pdaWaterMeterDao.getByBarcode(oldBarCode);
					 if (null!=pdaWaterMeter){
						 pdaWaterMeter.getPdaMdAssetInfo().setBarCode(barCode);
						 pdaWaterMeter.setLasteditby("eamService");
						 pdaWaterMeter.setLasteditdt(DateUtil.dateFormat( new Date()));
						 try{
							 pdaWaterMeterDao.merge(pdaWaterMeter);
							 result =true;
						 }
						 catch(Exception ex){
							 result = false;
							 logger.error("更新表PDA_WATER_METER中barcode为"+oldBarCode+"失败！", ex);
						 }
					 }
					 else{
						 result = false;
						 logger.warn("在PDA_WATER_METER表中不存在barcode为"+oldBarCode+"的数据！");
					 }
				 }
				 else if(classStructureId.equals("2")){
					 PdaAmMeter pdaAmMeter = pdaAmMeterDao.getByBarcode(oldBarCode);
					 pdaAmMeter.getPdaMdAssetInfo().setBarCode(barCode);
					 pdaAmMeter.setLasteditby("eamService");
					 pdaAmMeter.setLasteditdt(DateUtil.dateFormat(new Date()));
					 try{
						 result =true;
						 pdaAmMeterDao.merge(pdaAmMeter);
					 }
					 catch(Exception ex){
						 result = false;
						 logger.error("更新表PDA_AM_METER中barcode为"+oldBarCode+"失败！", ex);
					 }
				 }
				 else{
					 result = false;
					 logger.warn(barCode+","+assetNum+","+siteId+"在PDA_MD_ASSET_INFO表既不是水表的基础资料，也不是电表的基础资料！");
				 }
			 }
			 else{
				 result = false;
				 logger.warn("该记录"+barCode+","+assetNum+","+siteId+".在表PDA_METER_OPERATE中已经存在");
			 }
		}
		else{
			 result = false;
			 logger.warn("该记录"+oldBarCode+","+oldAssetNum+","+oldSiteId+".不是换表的信息，或者该记录在资产信息表中不存在");
		}
		return result;
	}

	
	
	
}
